Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merkle nodes with even-length paths made shorter #354

Closed
wants to merge 1 commit into from

Conversation

Scooletz
Copy link
Contributor

@Scooletz Scooletz commented Jun 13, 2024

This PR amends the way Leafs of Merkle trie with even paths are stored, so that if the path starts with a nibble in form of 0b11__ it will be one byte shorter to store. As 0b11 is a one of four prefixes, with a uniform distribution 1/4 of even paths should require one byte less to store.

The optimization uses the fact that Type.Leaf is encoded in first byte as 0b1000_0000. This plus the fact that other higher bits are not used by other cases, allows to use 0b11 as a discriminator of a leaf that has some property. In that case it's the property that the path starts with 0b11 which allows us to not store any additional data beside the leaf's NibblePath. The leaf type is encoded in the path itself.

Benchmark

Importing Sepolia db.

Before

image

After

image

@Scooletz Scooletz added 💾 disk size A change makes the disk size smaller 🌴Merkle Merkle construct 💥Breaking The change introduces a storage breaking change. labels Jun 13, 2024
Copy link

Code Coverage

Package Line Rate Branch Rate Health
Paprika 85% 79%
Summary 85% (3949 / 4658) 79% (1255 / 1592)

Minimum allowed line rate is 75%

@Scooletz Scooletz changed the title Merkle nodes with even-length paths starting with 0b11 will be shorter Merkle nodes with even-length paths made shorter Jun 13, 2024
@Scooletz
Copy link
Contributor Author

Little to no gains.

@Scooletz Scooletz closed this Jun 13, 2024
@Scooletz Scooletz deleted the merkle-leafs-with-even-paths-smaller branch June 13, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💥Breaking The change introduces a storage breaking change. 💾 disk size A change makes the disk size smaller 🌴Merkle Merkle construct
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant